﻿using Coolzon.UI.Common.Widgets.Properties;
using Coolzon.UI.Core;
using Coolzon.UI.Core.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Common.Widgets.Converters
{
    /*
     * DataSource 
     * 和
     * <DataSource Name="CDPDB">
        <DataSet Name="Test">
            <DataParameters>
                <DataParameter Name="p" Value="1"/>       
            </DataParameters>
        </DataSet>
        </DataSource>
     * 之间相互转换
     * */
    /// <summary>
    /// 数据源转换器        
    /// </summary>
    public class DataSourceConverter : ValueConverter
    {
        public object Convert(ConvertContext context, RT.Property property)
        {
            // 构建数据源
            DataSource dataSource = new DataSource();
            dataSource.Name = property.ValueNode.Attributes["Name"];
            bool isStatic = false;
            if (property.ValueNode.Attributes.ContainsKey("IsStatic"))
            {
                bool.TryParse(property.ValueNode.Attributes["IsStatic"], out isStatic);
            }
            dataSource.IsStatic = isStatic;

            if (property.ValueNode.ValueNodes != null && property.ValueNode.ValueNodes.Count > 0)
            {
                // 构建数据集
                RT.ValueNode dataSetNode = property.ValueNode.ValueNodes[0];
                DataSet dataSet = new DataSet();
                dataSet.Name = dataSetNode.Attributes["Name"];
                dataSource.DataSet = dataSet;
                dataSet.Parameters = new List<DataParameter>();

                if(dataSetNode.ValueNodes != null && dataSetNode.ValueNodes.Count > 0)
                {
                    RT.ValueNode parametersNode = null;
                    for (int index = 0; index < dataSetNode.ValueNodes.Count; index++)
                    {
                        RT.ValueNode node = dataSetNode.ValueNodes[index];
                        if (node.Name == "DataParameters")
                        {
                            parametersNode = node;
                            break;
                        }
                    }

                    // 构建参数
                    if (parametersNode.ValueNodes != null)
                    {
                        for (int index = 0; index < parametersNode.ValueNodes.Count; index++)
                        {
                            RT.ValueNode parameterNode = parametersNode.ValueNodes[index];
                            DataParameter parameter = new DataParameter();
                            parameter.Name = parameterNode.Attributes["Name"];
                            parameter.Type = parameterNode.Attributes.ContainsKey("Type") ? parameterNode.Attributes["Type"] : "IN";
                            parameter.ValueString = parameterNode.Attributes["Value"];
                            dataSet.Parameters.Add(parameter);
                        }
                    }
                }                
            }
            return dataSource;
        }

        public RT.ValueNode ConvertBack(ConvertContext context, object value, object dtTagData)
        {
            DataSource dataSource = (DataSource)value;
            if (dataSource == null)
            {
                return null;
            }

            RT.ValueNode dataSourceNode = new RT.ValueNode("DataSource");
            dataSourceNode.Attributes = new Dictionary<string, string>();
            dataSourceNode.Attributes.Add("Name", dataSource.Name);
            dataSourceNode.Attributes.Add("IsStatic", dataSource.IsStatic ? bool.TrueString : bool.FalseString);

            if(dataSource.DataSet != null)
            {
                RT.ValueNode dataSetNode = new RT.ValueNode("DataSet");
                dataSetNode.Attributes = new Dictionary<string, string>();
                dataSetNode.Attributes.Add("Name", dataSource.DataSet.Name);
                dataSetNode.ValueNodes = new List<RT.ValueNode>();

                if(dataSource.DataSet.Parameters != null && dataSource.DataSet.Parameters.Count > 0)
                {
                    RT.ValueNode parametersNode = new RT.ValueNode("DataParameters");
                    dataSetNode.ValueNodes.Add(parametersNode);
                    for (int index = 0; index < dataSource.DataSet.Parameters.Count; index++)
                    {
                        RT.ValueNode parameterNode = new RT.ValueNode("DataParameter");
                        parameterNode.Attributes = new Dictionary<string, string>();
                        parameterNode.Attributes.Add("Name", dataSource.DataSet.Parameters[index].Name);
                        parameterNode.Attributes.Add("Value", dataSource.DataSet.Parameters[index].ValueString);
                        parameterNode.Attributes.Add("Type", dataSource.DataSet.Parameters[index].Type);
                        parametersNode.ValueNodes = new List<RT.ValueNode>();
                        parametersNode.ValueNodes.Add(parameterNode);
                    }
                }

                dataSourceNode.ValueNodes = new List<RT.ValueNode>();
                dataSourceNode.ValueNodes.Add(dataSetNode);
            }
            return dataSourceNode;
        }
    }
}
